function updateDB(user, errors, successes) {
	
	var i;
	var uploaded;
	var sizeErr = errors.length;
	var sizeSuc = successes.length;
	var url;
	
	if (sizeErr > 0) {
		for (i = 0; i < sizeErr; i++) {
			url = errors[i].assertion;
			if (errors[i].reason == "EXISTS")
				uploaded = true;
			else
				uploaded = false;
			
			if (window.XMLHttpRequest) // code for IE7+, Firefox, Chrome, Opera, Safari
			{ 
				xmlhttp = new XMLHttpRequest();
			}
			else // code for IE6, IE5
			{ 
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			xmlhttp.onreadystatechange = function()
			{
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
				{
					window.location.reload();
					$response = xmlhttp.responseText;
//					if (!strcmp($response, "ERR")) {
//						document.getElementById("message").innerHTML = "Impossibile aggiornare DB";
//					} else {
//						document.getElementById("message").innerHTML = "DB aggiornato";
//					}
				}
			}

			xmlhttp.open("GET", "php/ajax/update_db.php?u=" + user + "&url=" + url + "&up=" + uploaded, true);
			xmlhttp.send();

		}
	}
	
	if (sizeSuc > 0) {
		uploaded = true;
		for (i = 0; i < sizeSuc; i++) {
			url = successes[i];
			
			if (window.XMLHttpRequest) // code for IE7+, Firefox, Chrome, Opera, Safari
			{ 
				xmlhttp = new XMLHttpRequest();
			}
			else // code for IE6, IE5
			{ 
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			xmlhttp.onreadystatechange = function()
			{
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
				{
					window.location.reload();
					$response = xmlhttp.responseText;
					document.getElementById("message").innerHTML = $response;
//					if (!strcmp($response, "ERR")) {
//						document.getElementById("message").innerHTML = "Impossibile aggiornare DB";
//					} else {
//						document.getElementById("message").innerHTML = "DB aggiornato";
//					}
				}
			}

			xmlhttp.open("GET", "php/ajax/update_db.php?u=" + user +  "&url=" + url + "&up=" + uploaded, true);
			xmlhttp.send();
		}

	}
}

function uploadBadges(urls, user) {
	OpenBadges.issue(urls, function(errors, successes) {
		updateDB(user, errors, successes);
	});
}